package com.study.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author sjw
 * @className Text
 * @date 2020/11/21 15:02
 * @description: TODO
 */
public class Text {

    private Integer i;
    private List list;

    public static void main(String[] args) throws Exception {
        String a = "user1";
        //switchFunc(a);
        //ifFunc(a);

        Text text=new Text();




        //f

        // ileds(text,T
        // ext.class);
        int num=1000000;
        long s1=System.currentTimeMillis();
        for (int i=0;i<num;i++){
            text.i=null;
            text.list=null;
            setDefaultValueIf(text,Text.class);
        }

        long s2=System.currentTimeMillis();
        for (int i=0;i<num;i++){
            text.i=null;
            text.list=null;
            setDefaultValueSwitch(text,Text.class);
        }
        long s3=System.currentTimeMillis();
        System.out.println(s2-s1);
        System.out.println(s3-s2);

    }

    private static void fileds(Object obj, Class aClass)  {
        for (Field superField : aClass.getDeclaredFields()) {


            System.out.println(superField.getGenericType().toString());
        }
    }

    public static void switchFunc(String input) {
        switch (input) {
            case "user":
                System.out.println("user");
                break;
            case "dept":
                System.out.println("dept");
                break;
            default:
                System.out.println("miss case");
        }
    }

    public static void ifFunc(String input) {
        if ("user".equals(input)) System.out.println("user");
        else if ("dept".equals(input)) System.out.println("dept");
        else System.out.println("miss if");
    }


    private static void setDefaultValueIf(Object obj, Class aClass) throws Exception {
        for (Field superField : aClass.getDeclaredFields()) {
            superField.setAccessible(true);
            if (Objects.isNull(superField.get(obj))) {
                if (superField.getGenericType().toString().equals("class java.lang.String")) {
                    superField.set(obj, "");
                } else if (superField.
                        getGenericType().toString().equals("class java.lang.Integer")) {
                    superField.set(obj, -1);
                } else if (superField.getGenericType().toString().equals("class java.lang.Double")) {
                    superField.set(obj, 0.0);
                } else if (superField.getGenericType().toString().equals("class java.lang.Boolean")) {
                    superField.set(obj, Boolean.FALSE);
                } else if (superField.getGenericType().toString().equals("class java.math.BigDecimal")) {
                    superField.set(obj, new BigDecimal(0));
                } else if (superField.getGenericType().toString().equals("class java.util.ArrayList")) {
                    superField.set(obj, new ArrayList<>());
                } else if (superField.getType().equals(List.class)) {
                    superField.set(obj, new ArrayList<>());
                }
            }
        }
    }


    private static void setDefaultValueSwitch(Object obj, Class aClass) throws Exception {
        for (Field superField : aClass.getDeclaredFields()) {
            if (!Objects.isNull(superField.get(obj))) continue;
            superField.setAccessible(true);

            switch (superField.getGenericType().toString()) {
                case "class java.lang.String":
                    superField.set(obj, "");
                    break;
                case "class java.lang.Integer":
                    superField.set(obj, -1);
                    break;
                case "class java.lang.Double":
                    superField.set(obj, 0.0);
                    break;
                case "class java.lang.Boolean":
                    superField.set(obj, Boolean.FALSE);
                    break;
                case "class java.math.BigDecimal":
                    superField.set(obj, new BigDecimal(0));
                    break;
                case "class java.util.ArrayList":
                    superField.set(obj, new ArrayList<>());
                    break;
                case "interface java.util.List":
                    superField.set(obj, new ArrayList<>());
                    break;
            }
        }
    }
}

